package com.sunwayhorizo.health.business.integration.excelTools;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;

import org.jboss.seam.Component;
import org.jboss.seam.annotations.Name;

import com.sunwayhorizo.health.business.dto.patad.empi.MedicalCardDTO;
import com.sunwayhorizo.health.business.dto.patad.empi.PatientBaseInfoDTO;
import com.sunwayhorizo.health.business.dto.registries.BedDTO;
import com.sunwayhorizo.health.business.dto.registries.DepartmentDTO;
import com.sunwayhorizo.health.business.dto.registries.EntitysDTO;
import com.sunwayhorizo.health.business.registries.organization.query.DepartmentQueryLocal;
import com.sunwayhorizo.health.common.StrUtil;
import com.sunwayhorizo.health.business.dto.DictionaryDTO;
import com.sunwayhorizo.health.ets.dto.EtConceptsDTO;
import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.rim.Organization;
import com.sunwayhorizo.health.hl7.rim.Role;

@Stateless
@Name("ExcelEtsTools")
public class ExcelEtsTools implements ExcelEtsToolsLocal {
	@Override
	public List<EtConceptsDTO> convertEtConceptsExcel(File file)
			throws BiffException, IOException {
		List<EtConceptsDTO> list = new ArrayList<EtConceptsDTO>();
		Workbook readwb = null;
		InputStream ips = null;
		// 对excel进行解析
		WorkbookSettings workbookSettings = new WorkbookSettings();
		workbookSettings.setEncoding("GBK");
		ips = new FileInputStream(file);
		readwb = Workbook.getWorkbook(ips, workbookSettings);
		Sheet sheet = readwb.getSheet(0);
		int rowSheet = sheet.getRows();
		// int colowSheet=sheet.getColumns();
		for (int i = 1; i < rowSheet; i++) {
			Cell cell1 = sheet.getCell(0, i);
			Cell cell2 = sheet.getCell(1, i);
			Cell cell3 = sheet.getCell(2, i);
			String conceptId = cell1.getContents();
			String conceptName = cell2.getContents();
			String conceptCode = cell3.getContents();
			// 组装对象 形成集合
			EtConceptsDTO dto = new EtConceptsDTO();
			dto.setConceptId(conceptId);
			dto.setConceptCode(conceptCode);
			dto.setConceptName(conceptName);
			// dto.setVersionId("1151");
			dto.setStatusCode("1");
			dto.setCreatedByName("李东南");
			list.add(dto);
		}
		readwb.close();
		return list;
	}

	@Override
	public List<EtConceptsDTO> convertEtConceptsExcel(File file,
			String versionId) throws FileNotFoundException, BiffException,
			IOException {
		List<EtConceptsDTO> list = new ArrayList<EtConceptsDTO>();
		Workbook readwb = null;
		InputStream ips = null;
		// 对excel进行解析
		WorkbookSettings workbookSettings = new WorkbookSettings();
		workbookSettings.setEncoding("GBK");
		ips = new FileInputStream(file);
		readwb = Workbook.getWorkbook(ips, workbookSettings);
		Sheet sheet = readwb.getSheet(0);
		int rowSheet = sheet.getRows();
		for (int i = 1; i < rowSheet; i++) {
			Cell cell1 = sheet.getCell(0, i);
			Cell cell2 = sheet.getCell(1, i);
			Cell cell3 = sheet.getCell(2, i);
			String conceptId = cell1.getContents();
			String conceptName = cell2.getContents();
			String conceptCode = cell3.getContents();
			// 组装对象 形成集合
			EtConceptsDTO dto = new EtConceptsDTO();
			dto.setConceptId(conceptId);
			dto.setConceptCode(conceptCode);
			dto.setConceptName(conceptName);
			dto.setVersionId(versionId);
			dto.setStatusCode("1");
			dto.setCreatedByName("李东南");
			list.add(dto);
		}
		readwb.close();
		return list;
	}

	@Override
	public List<DepartmentDTO> convertDepartmentExcel(File file)
			throws FileNotFoundException, BiffException, IOException {
		// 调用区分后集合
		List<DepartmentDTO> list = this.convertDepartmentExcelOne(file);
		for (int i = 0; i < list.size(); i++) {
			DepartmentDTO departmentDTO = list.get(i);
			String departmentCode = departmentDTO.getDepartmentCode();
			String parentCode = departmentDTO.getParentCode();
			// 取出父类科室
			if ("0000000".equals(parentCode)) {
				String departmentCodeOnetoThree = departmentCode
						.substring(0, 3);
				// 与其他子类进行判断
				for (int j = i + 1; j < list.size(); j++) {
					DepartmentDTO departmentDTOJ = list.get(j);
					String departmentCodeJ = departmentDTOJ.getDepartmentCode();
					String departmentCodeJOnetoThree = departmentCodeJ
							.substring(0, 3);
					if (departmentCodeOnetoThree
							.equals(departmentCodeJOnetoThree)) {
						departmentDTOJ.setParentCode(departmentCode);
					} else {
						break;
					}
				}
			}
		}
		return list;
	}

	// 判断 是否为父类科室 对其进行标记
	private List<DepartmentDTO> convertDepartmentExcelOne(File file)
			throws BiffException, IOException {
		List<DepartmentDTO> list = new ArrayList<DepartmentDTO>();
		Workbook readwb = null;
		InputStream ips = null;
		// 解析
		WorkbookSettings workbookSettings = new WorkbookSettings();
		workbookSettings.setEncoding("GBK");
		ips = new FileInputStream(file);
		readwb = Workbook.getWorkbook(ips, workbookSettings);
		Sheet sheet = readwb.getSheet(0);
		int rowSheet = sheet.getRows();
		for (int i = 1; i < rowSheet; i++) {
			Cell cell1 = sheet.getCell(0, i);
			Cell cell2 = sheet.getCell(1, i);
			Cell cell6 = sheet.getCell(5, i);
			Cell cell9 = sheet.getCell(8, i);
			String departmentCode = cell1.getContents();
			if (!departmentCode.startsWith("1")
					&& !departmentCode.startsWith("2")
					|| departmentCode.substring(1).endsWith("000000")) {
				continue;
			}
			String departmentName = cell2.getContents();
			String departmentType = cell6.getContents();
			if (departmentName.startsWith("停用")) {
				continue;
			}
			// 组装 判断
			DepartmentDTO departmentDTO = new DepartmentDTO();
			departmentDTO.setDepartmentCode(departmentCode);
			departmentDTO.setDepartmentName(departmentName);
			String inhos = cell9.getContents();

			if (inhos.equals("1")) {
				departmentDTO.getDepartmentType().setCodingschemeEnName(
						"DeptAttr");
				departmentDTO.getDepartmentType().setConceptCode("2");
				departmentDTO.getDepartmentType().setConceptName("住院");
			}

			if (departmentType.equals("1")) {
				departmentDTO.getDepartmentType().setCodingschemeEnName(
						"DeptAttr");
				departmentDTO.getDepartmentType().setConceptCode("0");
				departmentDTO.getDepartmentType().setConceptName("门诊");
			} else if (departmentType.equals("7")) {
				departmentDTO.getDepartmentType().setCodingschemeEnName(
						"DeptAttr");
				departmentDTO.getDepartmentType().setConceptCode("12");
				departmentDTO.getDepartmentType().setConceptName("药剂");
			} else if (departmentType.equals("9")) {
				departmentDTO.getDepartmentType().setCodingschemeEnName(
						"DeptAttr");
				departmentDTO.getDepartmentType().setConceptCode("12");
				departmentDTO.getDepartmentType().setConceptName("药剂");
			}

			if (departmentCode.equals("0000000")) {
				departmentDTO.setFlag("0");
			} else if (departmentCode.substring(3).equals("0000")) {
				departmentDTO.setParentCode("0000000");
				departmentDTO.setFlag("1");
			} else {
				departmentDTO.setFlag("2");
			}
			list.add(departmentDTO);
		}
		readwb.close();
		return list;
	}

	@Override
	public List<EntitysDTO> convertEntitysExcel(File file, int startNo,
			int catchNo) throws FileNotFoundException, BiffException,
			IOException {
		List<EntitysDTO> list = new ArrayList<EntitysDTO>();
		Workbook readwb = null;
		InputStream ips = null;
		// 解析excel
		WorkbookSettings workbookSettings = new WorkbookSettings();
		workbookSettings.setEncoding("GBK");
		ips = new FileInputStream(file);
		readwb = Workbook.getWorkbook(ips, workbookSettings);
		Sheet sheet = readwb.getSheet(0);
		@SuppressWarnings("unused")
		int rowSheet = sheet.getRows();
		for (int i = startNo; i < catchNo; i++) {
			Cell cell1 = sheet.getCell(0, i);
			Cell cell2 = sheet.getCell(1, i);
			Cell cell3 = sheet.getCell(2, i);
			Cell cell4 = sheet.getCell(3, i);
			Cell cell5 = sheet.getCell(4, i);
			Cell cell6 = sheet.getCell(6, i);
			Cell cell7 = sheet.getCell(11, i);
			// PatientBaseInfoDTO
			String natureId = cell1.getContents();
			String natureName = cell2.getContents();
			// PatientBaseInfoDTO-DictionaryDTO-
			String natureSexDic = cell3.getContents();
			// DepartmentDTO-
			String departmentCode = cell4.getContents();
			String departmentName = cell5.getContents();
			// PatientBaseInfoDTO-DictionaryDTO
			String conceptName = cell6.getContents();
			// id DictionaryDTO-conceptName
			String id = cell7.getContents();
			EntitysDTO entitysDTO = new EntitysDTO();
			// 进行组装前准备PatientBaseInfoDTO
			PatientBaseInfoDTO patientBaseInfoDTO = new PatientBaseInfoDTO();
			patientBaseInfoDTO.setNatureId(natureId);
			patientBaseInfoDTO.setNatureName(natureName);
			DictionaryDTO natureSexDic2 = new DictionaryDTO();
			natureSexDic2.setConceptCode(natureSexDic);
			patientBaseInfoDTO.setNatureSexDic(natureSexDic2);
			// 进行组装前准备DepartmentDTO
			DepartmentDTO departmentDTO = new DepartmentDTO();
			departmentDTO.setDepartmentId("12219");
			departmentDTO.setDepartmentCode(departmentCode);
			departmentDTO.setDepartmentName(departmentName);
			// 职称
			DictionaryDTO dutyDic = new DictionaryDTO();
			dutyDic.setConceptName(conceptName);
			patientBaseInfoDTO.setDutyDic(dutyDic);

			// 身份证
			MedicalCardDTO medicalCardDTO = new MedicalCardDTO();
			medicalCardDTO.setId(id);
			DictionaryDTO typeDic = new DictionaryDTO();
			typeDic.setConceptName("身份证");
			medicalCardDTO.setTypeDic(typeDic);
			// 组合
			entitysDTO.getOrganization().setOrgId("38687");
			entitysDTO.setPatientBaseInfo(patientBaseInfoDTO);
			entitysDTO.setDepartment(departmentDTO);
			List<MedicalCardDTO> listMedicalCard = new ArrayList<MedicalCardDTO>();
			listMedicalCard.add(medicalCardDTO);
			entitysDTO.setListMedicalCard(listMedicalCard);
			list.add(entitysDTO);
		}
		readwb.close();
		return list;
	}

	// 组装床位dto
	@Override
	public List<BedDTO> convertBedExcel(File file)
			throws FileNotFoundException, BiffException, IOException,
			Hl7Exception {
		List<BedDTO> bedDTOs = new ArrayList<BedDTO>();
		WorkbookSettings workbookSettings = new WorkbookSettings();
		workbookSettings.setEncoding("GBK");
		FileInputStream fis = new FileInputStream(file);
		Workbook readwb = Workbook.getWorkbook(fis, workbookSettings);
		Sheet sheet = readwb.getSheet(0);
		for (int i = 2; i < sheet.getRows(); i++) {
			BedDTO dto = new BedDTO();
			Cell cell1 = sheet.getCell(0, i);
			Cell cell2 = sheet.getCell(1, i);
			Cell cell3 = sheet.getCell(2, i);
			Cell cell4 = sheet.getCell(3, i);
			Cell cell5 = sheet.getCell(4, i);
			Cell cell9 = sheet.getCell(8, i);
			Cell cell10 = sheet.getCell(9, i);
			String canuse = cell5.getContents();
			if (StrUtil.isEmpty(canuse)) {
				continue;
			}
			String bedNo = cell1.getContents();
			String wardNo = cell2.getContents();
			String workGroupNo = cell3.getContents();
			@SuppressWarnings("unused")
			String departmentNo = cell4.getContents();
			@SuppressWarnings("unused")
			String status = cell9.getContents();
			String typeCode = cell10.getContents();
			dto.setBedNo(bedNo);

			dto.setStatus("0");
			dto.setWardId(wardNo);
		    DepartmentQueryLocal departmentManager = (DepartmentQueryLocal)Component.getInstance("DepartmentQuery");
			Role role = departmentManager.findWorkGroupRoleByWorkGroupCode(workGroupNo);
			if (role != null) {
				dto.setWorkGroupRoleId(role.getPlayerEntity().getEntityId()
						.toString());
			}
			dto.setSex("0");
			dto.getLevel().setConceptCode("J");
			dto.getLevel().setCodingschemeEnName("BedLevel");
			dto.getType().setConceptCode((Integer.parseInt(typeCode) - 1) + "");
			dto.getType().setCodingschemeEnName("BedApprovedType");
			bedDTOs.add(dto);
		}
		readwb.close();
		fis.close();
		return bedDTOs;

	}

	// 组装床位dto
	@Override
	public List<BedDTO> convertBedExcel(File file, int start, int size)
			throws FileNotFoundException, BiffException, IOException,
			Hl7Exception {
		List<BedDTO> bedDTOs = new ArrayList<BedDTO>();
		WorkbookSettings workbookSettings = new WorkbookSettings();
		workbookSettings.setEncoding("GBK");

		FileInputStream fis = new FileInputStream(file);
		Workbook readwb = Workbook.getWorkbook(fis, workbookSettings);
		Sheet sheet = readwb.getSheet(0);
		for (int i = start; i < start + size; i++) {
			BedDTO dto = new BedDTO();
			Cell cell1 = sheet.getCell(0, i);
			Cell cell2 = sheet.getCell(1, i);
			Cell cell3 = sheet.getCell(2, i);
			Cell cell4 = sheet.getCell(3, i);
			Cell cell5 = sheet.getCell(4, i);
			Cell cell9 = sheet.getCell(8, i);
			Cell cell10 = sheet.getCell(9, i);

			System.out.println(cell5);

			System.out.println(cell5.getContents());
			String bedNo = cell1 == null ? "" : cell1.getContents();

			String wardNo = cell2 == null ? "" : cell2.getContents();
			String workGroupNo = cell3 == null ? "" : cell3.getContents();
			@SuppressWarnings("unused")
			String departmentNo = cell4 == null ? "" : cell4.getContents();
			String status = cell9 == null ? "" : cell9.getContents();
			String typeCode = cell10 == null ? "" : cell10.getContents();
			dto.setBedNo(bedNo);
			dto.setStatus(status);
			dto.setWardId(wardNo);
			DepartmentQueryLocal departmentManager = (DepartmentQueryLocal)Component.getInstance("DepartmentQuery");
			String workGroupId = departmentManager
					.findWorkGroupRoleByWorkGroupCode(workGroupNo).getRoleId()
					.toString();
			dto.setWorkGroupRoleId(workGroupId);
			dto.getType().setConceptCode(typeCode);
			dto.getType().setCodingschemeEnName("BedApprovedType");
			System.out.println(dto);
			bedDTOs.add(dto);

		}
		readwb.close();
		fis.close();
		return bedDTOs;

	}

	@Override
	public List<EntitysDTO> convertEntitysExcel(File file)
			throws FileNotFoundException, BiffException, IOException,
			Hl7Exception {
		List<EntitysDTO> list = new ArrayList<EntitysDTO>();
		Workbook readwb = null;
		InputStream ips = null;
		// 解析excel
		WorkbookSettings workbookSettings = new WorkbookSettings();
		workbookSettings.setEncoding("GBK");
		ips = new FileInputStream(file);
		readwb = Workbook.getWorkbook(ips, workbookSettings);
		Sheet sheet = readwb.getSheet(0);
		int rowSheet = sheet.getRows();
		for (int i = 4; i < rowSheet; i++) {
			Cell cell1 = sheet.getCell(0, i);
			Cell cell2 = sheet.getCell(1, i);
			Cell cell3 = sheet.getCell(2, i);
			Cell cell4 = sheet.getCell(3, i);
			Cell cell5 = sheet.getCell(4, i);
			Cell cell6 = sheet.getCell(6, i);
			Cell cell7 = sheet.getCell(11, i);
			// PatientBaseInfoDTO
			String natureId = cell1.getContents();
			String natureName = cell2.getContents();
			// PatientBaseInfoDTO-DictionaryDTO-
			String natureSexDic = cell3.getContents();
			// DepartmentDTO-
			String departmentCode = cell4.getContents();
			String departmentName = cell5.getContents();
			// PatientBaseInfoDTO-DictionaryDTO
			String conceptName = cell6.getContents();
			// id DictionaryDTO-conceptName
			String id = cell7.getContents();
			EntitysDTO entitysDTO = new EntitysDTO();
			// 进行组装前准备PatientBaseInfoDTO
			PatientBaseInfoDTO patientBaseInfoDTO = new PatientBaseInfoDTO();
			patientBaseInfoDTO.setNatureId(natureId);
			patientBaseInfoDTO.setNatureName(natureName);
			DictionaryDTO natureSexDic2 = new DictionaryDTO();
			natureSexDic2.setConceptCode(natureSexDic);
			patientBaseInfoDTO.setNatureSexDic(natureSexDic2);
			// 进行组装前准备DepartmentDTO
			DepartmentDTO departmentDTO = new DepartmentDTO();
			//
			DepartmentQueryLocal departmentManager = (DepartmentQueryLocal)Component.getInstance("DepartmentQuery");
			Organization org = departmentManager
					.findDepartmentByCode(departmentCode);
			if (org != null) {
				departmentDTO.setDepartmentId(org.getEntityId().toString());
			}
			departmentDTO.setDepartmentCode(departmentCode);
			departmentDTO.setDepartmentName(departmentName);
			// 职称
			DictionaryDTO dutyDic = new DictionaryDTO();
			dutyDic.setConceptName(conceptName);
			patientBaseInfoDTO.setDutyDic(dutyDic);

			// 身份证
			MedicalCardDTO medicalCardDTO = new MedicalCardDTO();
			medicalCardDTO.setId(id);
			DictionaryDTO typeDic = new DictionaryDTO();
			typeDic.setConceptName("身份证");
			medicalCardDTO.setTypeDic(typeDic);
			// 组合
			entitysDTO.getOrganization().setOrgId("38687");
			entitysDTO.setPatientBaseInfo(patientBaseInfoDTO);
			entitysDTO.setDepartment(departmentDTO);
			entitysDTO.getWorkgroupCodes().add(
					departmentCode.substring(0, 4) + "100");
			List<MedicalCardDTO> listMedicalCard = new ArrayList<MedicalCardDTO>();
			listMedicalCard.add(medicalCardDTO);
			entitysDTO.setListMedicalCard(listMedicalCard);
			list.add(entitysDTO);
		}
		readwb.close();
		return list;
	}
}
